package org.zbclan.bf2.rcon.ui;

import javax.servlet.http.HttpServletRequest;

import org.zbclan.bf2.rcon.BF2RconAppRunner;

import com.vaadin.Application;
import com.vaadin.service.ApplicationContext.TransactionListener;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Link;

public class PlayerServerSelectComponent extends HorizontalLayout implements
		Button.ClickListener, TransactionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5296407923247478704L;

	private final Link p;
	// private final Button playerButton;
	private final Button serverButton;

	public PlayerServerSelectComponent(BF2RconAppRunner appRunner) {

		// String appletUrl = appRunner.getContext() + "/applet/";
		// p = new Link("scan local profiles", new ExternalResource(appletUrl
		// + "?cmd=listLocalProfiles"));
		// p.setTargetName(appletUrl);
		// p.setTargetBorder(Link.TARGET_BORDER_NONE);
		// p.setTargetWidth(400);
		// p.setTargetHeight(300);
		p = new Link();

		// playerButton = new Button("Player", (Button.ClickListener) this);
		serverButton = new Button("Server", (Button.ClickListener) this);

		addComponent(p);
		// addComponent(playerButton);
		addComponent(serverButton);

		// Hello c = new Hello();
		// c.setText("asdf123xxx");
		// addComponent(c);

		setSpacing(true);

		appRunner.getApp().getContext().addTransactionListener(this);

		return;
	}

	@Override
	public void buttonClick(ClickEvent event) {

		// Button button = event.getButton();
		// if (button == playerButton) {
		// // getWindow().executeJavaScript("listProfiles()");
		// // getWindow().open(new ExternalResource(""),"_blank");
		//
		// // BF2RconApplet applet = new BF2RconApplet(appRunner);
		// // appRunner.fireEvent(new DisplayEvent(applet));
		// // applet.display("listLocalProfiles");
		//
		// appRunner.fireEvent(new RunAppletEvent("cmd=listLocalProfiles"));
		// } else if (button == serverButton) {
		// }

		return;
	}

	@Override
	public void transactionStart(Application application, Object transactionData) {

		if (application.equals(getApplication())) {
			HttpServletRequest request = (HttpServletRequest) transactionData;
			String appletUrl = request.getContextPath() + "/applet/";
			p.setCaption("scan local profiles");
			p.setResource(new ExternalResource(appletUrl
					+ "?cmd=listLocalProfiles"));
			p.setTargetName(appletUrl);
			p.setTargetBorder(Link.TARGET_BORDER_NONE);
			p.setTargetWidth(400);
			p.setTargetHeight(300);
		}

		return;
	}

	@Override
	public void transactionEnd(Application application, Object transactionData) {
	}
}
